package de.armar.belt_timer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MenuPane extends JPanel {
	private static final long serialVersionUID = 1L;
	private JButton resetButton;
	private JButton upButton;
	private JButton downButton;
	private BeltTimer main;
	private boolean reset = false;
	private Timer reset_timer;
	private JButton checkAllButton;
	protected boolean checkall = false;
	private Timer checkall_timer;
	private JButton nameButton;

	public MenuPane(BeltTimer main) {
		this.main=main;
		this.setLayout(new GridLayout(5, 1));
		this.reset_timer = new Timer(3000, new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent evt) {
				MenuPane.this.reset_timer.stop();
				MenuPane.this.resetButton.setText("");
				MenuPane.this.resetButton.setIcon(new ImageIcon("./src/de/armar/belt_timer/icons/trash_empty.png"));
				MenuPane.this.resetButton.setForeground(Color.BLACK);
				MenuPane.this.reset=false;
			}
		});
		this.checkall_timer = new Timer(3000, new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent evt) {
				MenuPane.this.checkall_timer.stop();
				MenuPane.this.checkAllButton.setText("");
				MenuPane.this.checkAllButton.setIcon(new ImageIcon("./src/de/armar/belt_timer/icons/dialog_ok.png"));
				MenuPane.this.checkAllButton.setForeground(Color.BLACK);
				MenuPane.this.checkall=false;
			}
		});
		
		this.resetButton = new JButton(new ImageIcon("./src/de/armar/belt_timer/icons/trash_empty.png"));
		this.resetButton.setFocusPainted(false);
		this.resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(MenuPane.this.reset) {
					MenuPane.this.reset_timer.stop();
					MenuPane.this.resetButton.setText("");
					MenuPane.this.resetButton.setIcon(new ImageIcon("./src/de/armar/belt_timer/icons/trash_empty.png"));
					MenuPane.this.resetButton.setForeground(Color.BLACK);					
					MenuPane.this.main.reset();					
				}else{
					MenuPane.this.reset_timer.start();
					MenuPane.this.resetButton.setIcon(null);
					MenuPane.this.resetButton.setText("RESET?");
					MenuPane.this.resetButton.setForeground(Color.RED);					
				}
				MenuPane.this.reset=!MenuPane.this.reset;
			}
		});
		
		this.nameButton = new JButton(new ImageIcon("./src/de/armar/belt_timer/icons/pencil2.png"));
		this.nameButton.setFocusPainted(false);
		this.nameButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				edit_name();				
			}
		});
		
		this.checkAllButton = new JButton(new ImageIcon("./src/de/armar/belt_timer/icons/dialog_ok.png"));
		this.checkAllButton.setFocusPainted(false);
		this.checkAllButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(MenuPane.this.checkall ) {
					MenuPane.this.checkall_timer.stop();
					MenuPane.this.checkAllButton.setText("");
					MenuPane.this.checkAllButton.setIcon(new ImageIcon("./src/de/armar/belt_timer/icons/dialog_ok.png"));
					MenuPane.this.checkAllButton.setForeground(Color.BLACK);					
					MenuPane.this.main.checkall();					
				}else{
					MenuPane.this.checkall_timer.start();
					MenuPane.this.checkAllButton.setIcon(null);
					MenuPane.this.checkAllButton.setText("Check?");
					MenuPane.this.checkAllButton.setForeground(Color.RED);					
				}
				MenuPane.this.checkall=!MenuPane.this.checkall;
			}
		});
		this.add(resetButton);
		this.add(checkAllButton);
		this.upButton = new JButton(new ImageIcon("./src/de/armar/belt_timer/icons/list_add.png"));
		this.upButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MenuPane.this.main.increment();
			}
		});
		this.add(upButton);
		upButton.setFocusPainted(false);
		this.downButton = new JButton(new ImageIcon("./src/de/armar/belt_timer/icons/list_remove.png"));
		this.downButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MenuPane.this.main.decrement();
			}
		});
		this.add(downButton);
		this.add(nameButton);
		downButton.setFocusPainted(false);
		resetButton.setMinimumSize(new Dimension(300,1));
	}

	protected void edit_name() {
		String tmp = JOptionPane.showInputDialog(main,"Solar-System name?","Solar-System name?",JOptionPane.QUESTION_MESSAGE);
		if(tmp!=null) main.setTitle(tmp.toUpperCase()+" ("+this.main.mainTitle+")");
	}

}
